package a09_贪心算法;

import java.util.Arrays;

/**
 * <p>
 * a02_分发饼干复习2
 * </p>
 *
 * @author flyduck
 * @since 2025/2/9
 */
public class a02_分发饼干复习2 {

    public static void main(String[] args) {
        findContentChildren(new int[]{1,2,3}, new int[]{3});
    }

    public static int findContentChildren(int[] children, int[] foods) {

        if(foods.length == 0){
            return 0;
        }

        Arrays.sort(children);
        Arrays.sort(foods);

        int result = 0;
        int foodIdx = foods.length - 1;
        int childIdx = children.length - 1;
        //将最大的饼干分给饭量最大的孩子
        while (childIdx >= 0 && foodIdx >= 0){
            if(foods[foodIdx] >= children[childIdx]){//够吃
                result++;
                childIdx--;
                foodIdx--;
            }else {//不够吃，让下一个孩子尝试
                childIdx--;
            }
        }
        return result;
    }
}
